import sun.security.x509.AVA;

import java.util.Arrays;
import java.util.Scanner;

public class test3 {
    public static int n, m;
    public static int sum, avg;
    public static int[] value;
    public static int[] result;

    public static int[] fun(){
        result = new int[m];
        for (int i=0; i < m; i++)
            result[i] = 0;
        outer:while (true){
            int min = result[0];
            for (int i = 0; i < result.length; i++)
                if(!(min < result[i]))
                    min = result[i];

            for (int i = 0; i < value.length; i++){
                for (int j = 0; j < result.length; j++)
                {
                    if(!(min < result[j])) {
                        result[j] += value[i];
                        value = deleteFirst(value);
                        if (value.length > 0)
                            continue outer;
                        else
                            return result;
                    }
                }
            }
        }
    }

    public static int[] deleteFirst(int[] arr) {
        int[] temp = new int[arr.length - 1];
        System.arraycopy(arr, 1, temp, 0, temp.length);
        return temp;
    }



    public static void main(String[] args) {
        while (true){
            Scanner sc = new Scanner(System.in);
            System.out.println("Please Input n(0~1000) and m(0~100):(Input 'END' to STOP) ");
            String str = sc.nextLine();
            if (str.equals("END")){
                break;
            }
            else{
                String[] str1 = str.split(" ");
                n = Integer.parseInt(str1[0]);
                m = Integer.parseInt(str1[1]);
                if(n<0 || n > 1000 || m < 0 || m > 100 || n < m || str1.length > 2)
                    System.out.println("Wrong number, please input once again.");
                else {
                    value = new int[n];
                    boolean flag = true;
                    outer1:while (flag){
                        sum = 0;
                        avg = 0;
                        System.out.println("Please Input v(i)(0~1000):");
                        String[] str2 = sc.nextLine().split(" ");
                        int[] temp = new int[n];
                        for(int i =0; i < n; i++){
                            temp[i] = Integer.parseInt(str2[i]);
                            sum += temp[i];
                            if(value[i]<0 || value[i] > 1000){
                                System.out.println("Wrong number, please input once again.");
                                continue outer1;
                            }
                        }
                        Arrays.sort(temp);
                        for (int i = 0; i < n; i++)
                            value[i] = temp[n-i-1];
                        avg = sum / n;
                        flag = false;
                    }
                    fun();
                    int max = result[0];
                    int min = result[0];
                    for (int i = 0; i < result.length; i++)
                    {
                        if(max <= result[i])
                            max = result[i];
                        if(min >= result[i])
                            min = result[i];
                    }
                    System.out.println(max - min);
                }
            }
        }
    }
}
